
package com.compuvest.webstatus;

// <editor-fold defaultstate="collapsed" desc="import">
import java.util.Map;
import java.util.HashMap;
//import java.util.Iterator;
// </editor-fold>

public class WebStat implements java.io.Serializable {

// <editor-fold defaultstate="collapsed" desc="init variables">
    public final static int VISIT  = 0;
    public final static int ACTIVE = 1;

    public final static int YES_FILLED_FORM = 2;
    public final static int NOT_FILLED_FORM = 3;
    public final static int RESELLER        = 4;

    private int active;

    private int todayAllCust;
//    private int todayNewCust;
//    private int todayOldCust;
    private int todayVisit;
    private int todayOrders;

    private int totalCust;
    private int totalVisit;
    private int totalOrders;

    private int notFillForm;
    private int yesFillForm;
    private int reseller;

    private Map<String,AdvertInfo> advert;
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="WebStat()">

    public WebStat() {
        advert = new HashMap<>();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="increase()">

    public void increase(int param) {
        switch(param) {
            case VISIT:             active++; todayVisit++; totalVisit++; break;
            case YES_FILLED_FORM:   yesFillForm++; break;
            case NOT_FILLED_FORM:   notFillForm++; break;
            case RESELLER:          reseller++; todayAllCust++; break;
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="decrease()">

    public void decrease(int param) {
        switch(param) {
            case ACTIVE: active--; break;
        }
    }
// </editor-fold>

    // no need oldCust and newCust
    public void setActive(int active)                   { this.active = active; }
//    public void initTodayNewCustomer()                  { this.todayNewCust = todayAllCust - yesFillForm - notFillForm - reseller; }
    public void setTodayAllCustomer(int todayAllCust)   { this.todayAllCust = todayAllCust; }
//    public void initTodayOldCustomer()                  { todayOldCust = todayOrders - todayNewCust; }
//    public void setTodayOldCustomer(int todayOldCust)   { this.todayOldCust = todayOldCust; }
    public void setTodayVisit(int todayVisit)           { this.todayVisit = todayVisit; }
    public void setTodayOrders(int todayOrders)         { this.todayOrders = todayOrders; }

    public void setTotalCustomer(int totalCust)         { this.totalCust = totalCust; }
    public void setTotalVisit(int totalVisit)           { this.totalVisit = totalVisit; }
    public void setTotalOrders(int totalOrders)         { this.totalOrders = totalOrders; }
    public void setNotFillForm(int notFillForm)         { this.notFillForm = notFillForm; }
    public void setYesFillForm(int yesFillForm)         { this.yesFillForm = yesFillForm; }
    public void setReseller(int reseller)               { this.reseller = reseller; }

    public void initAdvertSite(String name, int visit, int orders, double amount) {
        advert.put(name, new AdvertInfo(visit, orders, amount));
    }

// <editor-fold defaultstate="collapsed" desc="increaseAdvertSite()">
    // Constructs <tt>increaseAdvertSite(String name)</tt>
    // visit++
    //  or add new advert site
    //
    // @param  name -

    public void increaseAdvertSite(String name) {

        if(advert.containsKey(name)) {
            advert.get(name).increaseVisit();
        }
        else {
            advert.put(name, new AdvertInfo(1, 0, 0d));
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="makeOrder()">

    public void makeOrder(String name, double amount, int newCustomer, boolean isReseller) {
        if(advert.containsKey(name)) {
            AdvertInfo adv = advert.get(name);
            adv.increaseOrders();
            adv.addAmount(amount);

            todayOrders++;
            totalOrders++;
            totalCust++;

            todayAllCust++;
/*            if(newCustomer == 0) {
                todayNewCust++;
            }
            else {
                if(isReseller) {
                    reseller++;
                }
                else {
                    todayOldCust++;
                }
            }*/
        }
        else {                         //(visit,order,amount)
            advert.put(name, new AdvertInfo(1, 1, amount));
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="clearWebStat()">

    public void clearWebStat() {
        // as is active, totalVisit, totalCust, totalOrders
        todayVisit = 0;
        todayOrders = 0;
        todayAllCust = 0;
//        todayNewCust = 0;
//        todayOldCust = 0;
        notFillForm = 0;
        yesFillForm = 0;
        reseller = 0;

        advert.clear();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getJSON()">
    public String getJSON() {

        StringBuilder json = new StringBuilder();
        json.append("{");
        json.append("'active':").append(active);
        json.append(",'todayAllCust':").append(todayAllCust);
        json.append(",'notFillForm':").append(notFillForm);
        json.append(",'yesFillForm':").append(yesFillForm);
        json.append(",'reseller':").append(reseller);
//        json.append(",'todayNewCust':").append(todayNewCust);
//        json.append(",'todayOldCust':").append(todayOldCust);
        json.append(",'totalCust':").append(totalCust);
        json.append(",'todayVisit':").append(todayVisit);
        json.append(",'totalVisit':").append(totalVisit);
        json.append(",'todayOrder':").append(todayOrders);
        json.append(",'totalOrder':").append(totalOrders);

        json.append(",adv:[");

//        String name;
//        Iterator enter = advert.keySet().iterator();
//        for(int idx = 0; enter.hasNext(); idx++) {
        int idx = 0;
        for(String name : advert.keySet()) {
//            name = (String)enter.next();
            if(idx++ != 0) json.append(',');
            json.append(advert.get(name).getJSON(name));
        }
        json.append("]}");

        return json.toString();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="toString()">

    @Override
    public String toString() {
        return getJSON();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="class AdvertInfo">

    class AdvertInfo {

        private int visit;
        private int orders;
        private double amount;

        public AdvertInfo() {
            this(0, 0, 0d);
        }

        public AdvertInfo(int visit, int orders, double amount) {
            this.visit = visit;
            this.orders = orders;
            this.amount = amount;
        }

        public void increaseVisit()             { this.visit++; }
        public void increaseOrders()            { this.orders++; }
        public void addAmount(double amount)    { this.amount += amount; }

        public String getJSON(String name) {
            return "{n:'" + name + "',v:" + visit + ",o:" + orders + ",a:" + amount + ",u:0}";
        }

        @Override
        public String toString() {
            return "{v:" + visit + ",o:" + orders + ",a:" + amount + ",u:0}";
        }
    }
// </editor-fold>
}
